package com.ds.web.controller.applet;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ds.common.annotation.Log;
import com.ds.common.core.controller.BaseController;
import com.ds.common.core.domain.AjaxResult;
import com.ds.common.enums.BusinessType;
import com.ds.applet.domain.MiniBackdrop;
import com.ds.applet.service.IMiniBackdropService;
import com.ds.common.utils.poi.ExcelUtil;
import com.ds.common.core.page.TableDataInfo;

/**
 * 背景图Controller
 *
 * @author liucan
 * @date 2024-04-04
 */
@RestController
@RequestMapping("/applet/backdrop")
public class MiniBackdropController extends BaseController {
    @Resource
    private IMiniBackdropService miniBackdropService;

    @Resource
    private MiniLeagueController miniLeagueController;

    /**
     * 查询背景图列表
     */
    @GetMapping("/list")
    public TableDataInfo list(MiniBackdrop miniBackdrop, HttpServletRequest request) {
        startPage();
        miniBackdrop.setToApplet(miniLeagueController.toApplet(request));
        List<MiniBackdrop> list = miniBackdropService.selectMiniBackdropList(miniBackdrop);
        return getDataTable(list);
    }

    /**
     * 导出背景图列表
     */
    @Log(title = "背景图", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MiniBackdrop miniBackdrop) {
        List<MiniBackdrop> list = miniBackdropService.selectMiniBackdropList(miniBackdrop);
        ExcelUtil<MiniBackdrop> util = new ExcelUtil<MiniBackdrop>(MiniBackdrop.class);
        util.exportExcel(response, list, "背景图数据");
    }

    /**
     * 获取背景图详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(miniBackdropService.selectMiniBackdropById(id));
    }

    /**
     * 新增背景图
     */
    @Log(title = "背景图", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MiniBackdrop miniBackdrop, HttpServletRequest request) {
        miniBackdrop.setToApplet(miniLeagueController.toApplet(request));
        int i = miniBackdropService.insertMiniBackdrop(miniBackdrop);
        if (i < 0) {
            return AjaxResult.error("已存在显示的背景图，不可重复添加！");
        }
        return toAjax(i);
    }

    /**
     * 修改背景图
     */
    @Log(title = "背景图", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MiniBackdrop miniBackdrop, HttpServletRequest request) {
        miniBackdrop.setToApplet(miniLeagueController.toApplet(request));
        int i = miniBackdropService.updateMiniBackdrop(miniBackdrop);
        if (i < 0) {
            return AjaxResult.error("已存在显示的背景图，不可同时存在两个！");
        }
        return toAjax(i);
    }

    /**
     * 删除背景图
     */
    @Log(title = "背景图", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(miniBackdropService.deleteMiniBackdropByIds(ids));
    }
}
